#!/bin/bash

date
card_id=$1

devid=`expr $card_id \* 4`

sn=`speclk $devid 3 | awk '{print $5}'`
echo $sn

SAVEDLOG=$2/$sn
mkdir -p $SAVEDLOG
log_file=$SAVEDLOG/4_core_log.log_${card_id}
if [ -e $log_file ];then
    rm -rf $log_file
fi

touch $log_file

CUR_PATH=`pwd`

DNN_PATH=dnn-test/build
SDAA_PATH=sdaatest/build
PCIE_PATH=pcietest/build
DNN_LOG=dnn-test/log
SDAA_LOG=sdaatest/log
PCIE_LOG=pcietest/log
HBM_PATH=hbmtest/build
HBM_LOG=hbmtest/log


function run_dnn_card(){
    rm -rf $DNN_LOG
    mkdir -p $DNN_LOG
    cd $DNN_PATH
    echo "dnn run start..." 

    start_device=`expr $card_id \* 4`
    end_device=`expr $card_id \* 4 + 3`

    # Run all card
    for device_id in `seq ${start_device} ${end_device}`
    do
            echo device_id:$device_id  
            sh dnn_run.sh $sn $device_id --card_id_$card_id &
    done

    wait

    for device_id in `seq 0${start_device} ${end_device}`
    do
            sh dnn_check.sh $sn ${device_id} --card_id_$card_id 
    done
    echo "dnn run finish!!!" 

    cd $CUR_PATH
}


function run_sdaa_card(){
    cd $SDAA_PATH && cd ..> /dev/null
    echo "sdaa run start..." | tee -a $log_file

    mkdir -p run-sdaa
    if [ -d run-sdaa/build_${card_id} ];then
        rm -rf run-sdaa/build_${card_id}
    fi

    cp -r build run-sdaa/build_${card_id} && cd run-sdaa/build_${card_id}
    pwd

    echo card_id:$card_id  
    sh sdaa-run.sh $card_id $sn --card_id_$card_id
    sh sdaa_check.sh $card_id $sn --card_id_$card_id 

    echo "sdaa run finish!!!" 

    cd $CUR_PATH
}


function run_pcie_card(){
    cd $PCIE_PATH
    echo "pcie run start..." 

    start_device=`expr $card_id \* 4`
    end_device=`expr $card_id \* 4 + 3`

    ph_card=`expr $card_id`

    # Run all card
    for device_id in `seq ${start_device} ${end_device}`
    do
            echo device_id:$device_id  
            sh pcie_run.sh $device_id $sn --card_id_$card_id &
    done

    wait

    for device_id in `seq 0${start_device} ${end_device}`
    do
            sh pcie_check.sh ${device_id} $sn --card_id_$ph_card 
    done

    echo "pcie run finish!!!" 
    cd - > /dev/null
}


function run_hbm_card(){
    cd $HBM_PATH
    echo "hbm run start..." 


    start_device=`expr $card_id \* 4`
    end_device=`expr $card_id \* 4 + 3`

    ph_card=`expr $card_id`

    # Run all card
    for device_id in `seq ${start_device} ${end_device}`
    do
            echo device_id:$device_id  
            sh hbm_run.sh ${device_id} $sn --card_id_$ph_card $ph_card &
    done

    wait

    for device_id in `seq 0${start_device} ${end_device}`
    do
            sh hbm_check.sh ${device_id} $sn --card_id_$ph_card 
    done
    echo "hbm run finish!!!" 

    cd - > /dev/null
}


function run(){
        run_sdaa_card
        sleep 5
        run_dnn_card
        sleep 5
        run_pcie_card
        sleep 5
        run_hbm_card
}


sleep 5

echo ""
echo -----------TEST card_id:$card_id------------- 
run

#log save
mkdir -p $SAVEDLOG/dnntest/
cp -r ${DNN_LOG}/$sn/$sn ${SAVEDLOG}/dnntest/
mkdir -p $SAVEDLOG/sdaatest/
cp -r ${SDAA_LOG}/$sn/$sn ${SAVEDLOG}/sdaatest/
mkdir -p $SAVEDLOG/pcietest/
cp -r ${PCIE_LOG}/$sn ${SAVEDLOG}/pcietest/
mkdir -p $SAVEDLOG/hbmtest/
cp -r ${HBM_LOG}/${CURTIME} ${SAVEDLOG}/hbmtest/

echo "------------TEST REPORT------------------------" 
result=$(cat $log_file | grep  'FAILED')
length=$(expr length "$result")
if [[ length -gt 0 ]];
then
        echo -e "CARD_$card_id TEST RESULT: \E[31m [FAILED] \E[0m" 
else
        echo -e "CARD_$card_id TEST RESULT: \E[32m [PASS] \E[0m" 
fi
